#include "gofgame/dungeonkeeper/game/plate.h"

using namespace gofgame::dungeonkeeper::game;

Plate::Plate(){
	maxNum = 0;
	currentNum = 0;
}

Plate * Plate::createPlate(float max,float current){
	Plate *plate = new Plate();
	if(plate!=nullptr){
		plate->setMaxNum(max);
		plate->setCurrentNum(current);
		if(plate->init()){
			return plate;
		} else {
			return nullptr;
		}
	}else {
		return nullptr;
	}
}

Plate * Plate::createPlate(){
	Plate *plate = new Plate();
	if(plate!=nullptr){
		if(plate->init()){
			return plate;
		} else {
			return nullptr;
		}
	}else {
		return nullptr;
	}
}

bool Plate::init(){
	bool bRet = false;
	do 
	{
		CC_BREAK_IF(!CCSprite::init());
		this->initWithFile("plateBorder.png");
		CCSprite *img = CCSprite::create("plateRect.png");
		cp = CCProgressTimer::create(img);
		CCSize size = this->getContentSize();
		cp->setType(kCCProgressTimerTypeBar);
		cp->setScaleY(1.1f);
		cp->setMidpoint(ccp(0,0.5));
		cp->setPercentage(100);
		cp->setPosition(ccp(size.width*0.5,size.height*0.5));
		this->addChild(cp,1);

		this->scheduleUpdate();
		bRet = true;
	} while (0);
	return bRet;
}

void Plate::update(float dt){
	if(maxNum!=0){
		if(currentNum <= 0){
			cp->setPercentage(0);
		}else{
			float rate = (currentNum / maxNum) * 100;
			cp->setPercentage(rate);
		}
	}
}